package uy.edu.um.ltic.server.business;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import sun.security.jca.GetInstance.Instance;
import uy.edu.um.ltic.exception.DataBaseException;
import uy.edu.um.ltic.server.communication.ServerSocket;
import uy.edu.um.ltic.server.persistence.ConferenceRemStarDAOFactory;

public class ConferenceReminderStarter {

	private static ConferenceReminderStarter instance;
	
	private ConferenceReminderStarter(){
		
	}
	
	public static ConferenceReminderStarter getInstance(){
		if (instance == null){
			instance = new ConferenceReminderStarter();
		}
		return instance;
	}
	
	
	@SuppressWarnings("deprecation")
	/*public  void scheduleReminder(final int idConf) throws DataBaseException{
		   // creating timer task, timer
	      TimerTask tasknew = new TimerTask(){

			@Override
			public void run() {
				System.out.println("asda");
				try {
					remindConference(idConf);
				} catch (DataBaseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
	      
	      };
	      Timer timer = new Timer();
	      Timestamp stamp = ConferenceFactory.getInstance().getConferenceMgt().getConfDate(idConf);
	      System.out.println(stamp.getTime());
	      stamp.setHours(stamp.getHours() - 1);
	      System.out.println(stamp.getTime());
	      Date schDate = new Date(stamp.getTime());
	      if (stamp.before(new Timestamp(System.currentTimeMillis()))){
	    	  remindConference(idConf);
	      }else{
	    	  timer.schedule(tasknew, schDate);  
	      }
	  
	}
	*/
	public  void scheduleStart(final int idConf) throws DataBaseException{
		   // creating timer task, timer
	      TimerTask tasknew = new TimerTask(){

			@Override
			public void run() {
				try {
					startConference(idConf);
					} catch (DataBaseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
	      
	      };
	      Timer timer = new Timer();
	      Timestamp stamp = ConferenceFactory.getInstance().getConferenceMgt().getConfDate(idConf);
	      Date schDate = new Date(stamp.getTime());
	      timer.schedule(tasknew, schDate);  
	   
	  
	}
	
	public void startConference(final int idConf) throws DataBaseException{
		ArrayList<Employee> part = ConferenceFactory.getInstance().getConferenceMgt().getAllParticipants(idConf);
		for(Employee emp : part){
			ServerSocket.notifyClients(new Integer(emp.getnEmployee()).toString(), new Integer(idConf).toString(), "startConf");
		}
	}
	
	public void remindConference(final int idConf) throws DataBaseException{
		ArrayList<Employee> part = ConferenceFactory.getInstance().getConferenceMgt().getAllParticipants(idConf);
		for(Employee emp : part){
			ServerSocket.notifyClients(new Integer(emp.getnEmployee()).toString(), new Integer(idConf).toString(), "remConf");
		}
	}
}
